<template>
  <el-dialog :title="dialogProps.title" :visible.sync="dialogProps.visible" width="560px"
    @open="handleDialogOpen()" v-loading="loading">
    <div slot="title" class="dialog-header">{{ dialogProps.title }}</div>
    <el-form :model="HealthPlanModel"
      ref="HealthPlanForm" label-width="80px" label-position="right" size="small">
      <el-form-item label="方案标题" prop="title">
        <el-col :span="16"><el-input :maxlength="32" v-model="HealthPlanModel.title" autofocus></el-input></el-col>
      </el-form-item>
      <el-form-item label="关键字" prop="keywords">
        <el-col :span="16"><el-input :maxlength="32" v-model="HealthPlanModel.keywords"></el-input></el-col>
      </el-form-item>
      <el-form-item label="方案类别" prop="typeId">
          <el-select v-model="HealthPlanModel.typeId" placeholder="请选择活动区域">
          <el-option
          v-for="item in parentTypeList"
          :key="item.id"
          :label="item.name"
          :value="item.id">
            </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="内容" prop="content">
        <el-input type="textarea" v-model="HealthPlanModel.content"></el-input>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
         <el-input type="textarea" v-model="HealthPlanModel.remark"></el-input>
      </el-form-item>
      <el-form-item label="状态" prop="state">
      <el-switch
        v-model="HealthPlanModel.state"
        active-text="启用"
        inactive-text="禁用"
        :active-value="0" :inactive-value="1"
        >
      </el-switch>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button size="small" @click="handleDialogClose()">取 消</el-button>
      <el-button type="primary" size="small" @click="submitForm('HealthPlanForm')">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { ThealthplanAdd,ThealthplanUpd} from '@/api/jiankangfangan_tj'

export default {
  name: 'jkfa-edit',
  props: ['parentTypeList'],
  data() {
    return {
      loading: false,
      HealthPlanModel: {

      },
      dialogProps: {
        visible: false,
        action: '',
        title: ''
      },
      }
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          if(this.dialogProps.action === 'add') {
            this.doSaveJk()
          } else {
            this.doUpdateJk()
          }
        } else {
          return false
        }
      });
    },
    doUpdateJk() {
      this.loading = true
      ThealthplanUpd(this.HealthPlanModel)
      .then(response => {
        this.loading = false
        this.dialogProps.visible = false
        this.$emit('save-finished')
      })
      .catch(error => {
        this.outputError(error)
      })
    },
    doSaveJk() {
      this.loading = true
      console.log(this.HealthPlanModel)
      ThealthplanAdd(this.HealthPlanModel)
      .then(response => {
        this.loading = false
        this.dialogProps.visible = false
        this.$emit('save-finished')
      })
      .catch(error => {
        this.outputError(error)
      })
    },
    handleDialogClose() {
      this.dialogProps.visible = false
    },
    handleDialogOpen() {
      this.$nextTick(() => {
        this.$refs['HealthPlanForm'].clearValidate()
      })
    },
    outputError(error) {
      console.log(error.response ? error.response : error.message)
      this.loading = false
      this.$message({
        showClose: true,
        message: '出错了，请按F12查看浏览器日志。',
        type: 'error'
      })
    }
  },
  mounted: function() {
    this.$nextTick(() => {
      this.$on('openEditDialog', function(THealthPlan) {
        this.HealthPlanModel=THealthPlan
        console.log(this.HealthPlanModel)
        this.dialogProps.action = 'edit'
        this.dialogProps.title = '修改'
        this.dialogProps.visible = true
      })
      this.$on('openAddDialog', function() {
        this.dialogProps.action = 'add'
        this.dialogProps.title = '新增'
        this.dialogProps.visible = true
      })
    })
  }
}
</script>

